package com.huawei.java.main.classes;


public class Server {
    protected final String type;
    protected final int cpu;
    protected final int memory;
    protected final int price;
    protected final int cost;
    protected final double ratio;

    public Server(String type, int cpu, int memory, int price, int cost) {
        this.type = type;
        this.cpu = cpu;
        this.memory = memory;
        this.price = price;
        this.cost = cost;
        this.ratio = (double) cpu / memory;
    }

    public Server(String describe) {
        final String substring = describe.substring(1, describe.length() - 1);
        final String[] parts = substring.split("\\s*,\\s*");
        this.type = parts[0];
        this.cpu = Integer.parseInt(parts[1]);
        this.memory = Integer.parseInt(parts[2]);
        this.price = Integer.parseInt(parts[3]);
        this.cost = Integer.parseInt(parts[4]);
        this.ratio = (double) cpu / memory;
    }

    public String getType() {
        return type;
    }

    public int getCpu() {
        return cpu;
    }

    public int getMemory() {
        return memory;
    }

    public int getPrice() {
        return price;
    }

    public int getCost() {
        return cost;
    }

    public double getRatio() {
        return ratio;
    }

    public ServerInstance createInstance() {
        return new ServerInstance(this);
    }

    public boolean fitService(Service service) {
        final int cpu = service.getCpu();
        final int memory = service.getMemory();
        if (service.isDoubleDeploy()) {
            return getCpu() >= cpu
                    && getMemory() >= memory;
        } else {
            return getCpu() / 2 >= cpu
                    && getMemory() / 2 >= memory;
        }
    }
}
